import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";

import { cn } from "@/lib/utils.ts";

const buttonVariants = cva(
  "hik-inline-flex hik-items-center hik-justify-center hik-whitespace-nowrap hik-rounded-md hik-text-sm hik-font-medium hik-ring-offset-background hik-transition-colors focus-visible:hik-outline-none focus-visible:hik-ring-2 focus-visible:hik-ring-ring focus-visible:hik-ring-offset-2 disabled:hik-pointer-events-none disabled:hik-opacity-50",
  {
    variants: {
      variant: {
        default:
          "hik-bg-primary hik-text-primary-foreground hover:hik-bg-primary/90",
        destructive:
          "hik-bg-destructive hik-text-destructive-foreground hover:hik-bg-destructive/90",
        outline:
          "hik-border hik-border-input hik-bg-background hover:hik-bg-accent hover:hik-text-accent-foreground",
        secondary:
          "hik-bg-secondary hik-text-secondary-foreground hover:hik-bg-secondary/80",
        ghost: "hover:hik-bg-accent hover:hik-text-accent-foreground",
        link: "hik-text-primary hik-underline-offset-4 hover:hik-underline",
      },
      size: {
        default: "hik-h-10 hik-px-4 hik-py-2",
        sm: "hik-h-9 hik-rounded-md hik-px-3",
        lg: "hik-h-11 hik-rounded-md hik-px-8",
        icon: "hik-h-10 hik-w-10",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  },
);

export interface ButtonProps
  extends React.ButtonHTMLAttributes<HTMLButtonElement>,
    VariantProps<typeof buttonVariants> {
  asChild?: boolean;
}

const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  ({ className, variant, size, asChild = false, ...props }, ref) => {
    const Comp = asChild ? Slot : "button";
    return (
      <Comp
        className={cn(buttonVariants({ variant, size, className }))}
        ref={ref}
        {...props}
      />
    );
  },
);
Button.displayName = "Button";

export { Button, buttonVariants };
